package com.maotouying.owlsmart.MainPage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.maotouying.owlsmart.InitActivity;
import com.maotouying.owlsmart.MainPage.fragment.HomeFragment;
import com.maotouying.owlsmart.MainPage.fragment.MessageFragment;
import com.maotouying.owlsmart.MainPage.fragment.UserFragment;
import com.maotouying.owlsmart.R;
import com.maotouying.owlsmart.SmartUtil.Http.localSqlite;
import com.maotouying.owlsmart.SmartUtil.SystemUtil;

import java.util.ArrayList;
import java.util.List;

public class FragmentManagerActivity extends AppCompatActivity {
    private List<Object> mFragmentList = new ArrayList<>();
    private FrameLayout mFlContent;
    private BottomBarLayout mBottomBarLayout;
    private RotateAnimation mRotateAnimation;
    private Handler mHandler = new Handler();

    private Boolean userState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_manager);

        //顶部框颜色
        SystemUtil.setStatusBarColor(this, this.getResources().getColor(R.color.top_background_color));

        //读取判断数据库是否存在数据，如果不存在，吊起全屏动画---->登录--->主页
        //否则获取第一个用户，如果token正确，则继续

        //获取临时表数据
        localSqlite user= new localSqlite();
        String userinfo= user.getPresent(FragmentManagerActivity.this);
        System.out.println("userinfo::"+userinfo);

        if("[]".equals(userinfo)){
            //数据表没有数据，重启程序至init
            Intent intent = new Intent(FragmentManagerActivity.this, InitActivity.class);
            startActivity(intent);

        }



        initView();
        initData();
        initListener();
    }


    @Override
public boolean onKeyDown(int keyCode,KeyEvent event){
if(keyCode== KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}//屏蔽返回键


    private void initView() {

        mFlContent = (FrameLayout) findViewById(R.id.fl_content);
        mBottomBarLayout = (BottomBarLayout) findViewById(R.id.bbl);


    }

    private void initData() {

        //首页
        HomeFragment homeFragment = new HomeFragment();
        mFragmentList.add(homeFragment);
        //聊天消息
        MessageFragment messageFragment = new MessageFragment();
        mFragmentList.add(messageFragment);
        //用户
        UserFragment userFragment = new UserFragment();
        mFragmentList.add(userFragment);


        changeFragment(0); //默认显示第一页
    }

    private void initListener() {
        mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
                Log.i("Init_Activity", "position: " + currentPosition);

                changeFragment(currentPosition);

                if (currentPosition == 0) {
                    //如果是第一个，即首页
                    if (previousPosition == currentPosition) {
                        //如果是在原来位置上点击,更换首页图标并播放旋转动画
                        if (mRotateAnimation != null && !mRotateAnimation.hasEnded()) {
                            //如果当前动画正在执行
                            return;
                        }

                        //  bottomBarItem.setSelectedIcon(R.mipmap.tab_loading);//更换成加载图标 setResId   iii

                        //播放旋转动画
                        if (mRotateAnimation == null) {
                            mRotateAnimation = new RotateAnimation(0, 360,
                                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                                    0.5f);
                            mRotateAnimation.setDuration(800);
                            mRotateAnimation.setRepeatCount(-1);
                        }
                        ImageView bottomImageView = bottomBarItem.getImageView();
                        bottomImageView.setAnimation(mRotateAnimation);
                        bottomImageView.startAnimation(mRotateAnimation);//播放旋转动画

                        //模拟数据刷新完毕
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
                                // bottomBarItem.setSelectedIcon(R.drawable.tab_home_selected);//更换成首页原来选中图标
                                cancelTabLoading(bottomBarItem);
                            }
                        }, 3000);
                        return;
                    }
                }

                //如果点击了其他条目
                BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
                //bottomItem.setSelectedIcon(R.drawable.tab_home_selected);//更换为原来的图标
                cancelTabLoading(bottomItem);//停止旋转动画
            }
        });

        mBottomBarLayout.setUnread(0, 0);//设置第一个页签的未读数为20
        mBottomBarLayout.setUnread(0, 0);//设置第二个页签的未读数

    }

    private void changeFragment(int currentPosition) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_content, (Fragment) mFragmentList.get(currentPosition));
        transaction.commit();
    }

    /**
     * 停止首页页签的旋转动画
     */
    private void cancelTabLoading(BottomBarItem bottomItem) {
        Animation animation = bottomItem.getImageView().getAnimation();
        if (animation != null) {
            animation.cancel();
        }
    }
}